package com.ikun.starters.jwt.impl;

import com.ikun.starters.jwt.IJwtMemberDataService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;

import java.util.Map;

/**
 * @author ximeng
 * @title: JwtMemberDataService
 * @description: token解析数据的类
 */
public class JwtMemberDataService implements IJwtMemberDataService {
    //过期时间
    private Integer expireTime=2592000;
    //token的签名
    private String tokenSignKey="ikun";

    public JwtMemberDataService(Integer expireTime,String tokenSignKey){
        this.expireTime=expireTime;
        this.tokenSignKey=tokenSignKey;
    }
    @Override
    public String getUserId(String token) {
        return parse(token).getBody().getId();
    }

    @Override
    public String getUsername(String token) {
        return parse(token).getBody().getSubject();
    }

    @Override
    public Map<String, Object> getHeaders(String token) {
        return parse(token).getHeader();
    }

    @Override
    public Jws<Claims> parse(String token) {
        return Jwts.parser().setSigningKey(tokenSignKey)
                .parseClaimsJws(token);
    }
}
